﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Sanford.Multimedia.Midi;

namespace WiiDrumSynth
{
    /// <summary>
    /// The options form n the WiiDrumSynth Application
    /// </summary>
    internal partial class MidiOptions : Form
    {
        int outDeviceId = 0;
        int channelId = 9;
        int programId = 0;

        /// <summary>
        /// Initializes a new instance of the <see cref="MidiOptions"/> class.
        /// </summary>
        internal MidiOptions()
        {
            InitializeComponent();

            PopulateOutputDevices();
            PopulateGMInstruments();
        }

        /// <summary>
        /// Populates the output devices.
        /// </summary>
        private void PopulateOutputDevices()
        {
            outputDeviceCombo.Items.Clear();
            if (OutputDevice.DeviceCount > 0)
            {
                for (int i = 0; i < OutputDevice.DeviceCount; i++)
                {
                    outputDeviceCombo.Items.Add(OutputDevice.GetDeviceCapabilities(i).name);
                }
                outputDeviceCombo.SelectedIndex = outDeviceId;
            }
        }

        /// <summary>
        /// Populates the GM instruments.
        /// </summary>
        private void PopulateGMInstruments()
        {
            gmMidiInstrument.Items.Clear();
            for (int i = 0; i < 127; i++)
            {
                gmMidiInstrument.Items.Add((GeneralMidiInstrument)i);
            }
        }

        /// <summary>
        /// Gets or sets the out device id.
        /// </summary>
        /// <value>The out device id.</value>
        internal int OutDeviceId
        {
            get { return outDeviceId; }
            set
            {
                outDeviceId = value;
                outputDeviceCombo.SelectedIndex = outDeviceId;
            }
        }

        /// <summary>
        /// Gets or sets the channel id.
        /// </summary>
        /// <value>The channel id.</value>
        internal int ChannelId
        {
            get { return channelId; }
            set
            {
                channelId = value;
                midiChannel.Value = value + 1;
            }
        }

        /// <summary>
        /// Gets or sets the program id.
        /// </summary>
        /// <value>The program id.</value>
        internal int ProgramId
        {
            get { return programId; }
            set
            {
                programId = value;
                midiInstrument.Value = value + 1;
                gmMidiInstrument.SelectedIndex = value;
            }
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the comboBox1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            outDeviceId = outputDeviceCombo.SelectedIndex;
        }

        /// <summary>
        /// Handles the ValueChanged event of the midiChannel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void midiChannel_ValueChanged(object sender, EventArgs e)
        {
            channelId = (int)midiChannel.Value - 1;
        }

        /// <summary>
        /// Handles the ValueChanged event of the midiInstrument control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void midiInstrument_ValueChanged(object sender, EventArgs e)
        {
            programId = (int)midiInstrument.Value - 1;
            gmMidiInstrument.SelectedIndex = programId;
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the gmMidiInstrument control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void gmMidiInstrument_SelectedIndexChanged(object sender, EventArgs e)
        {
            programId = gmMidiInstrument.SelectedIndex;
            midiInstrument.Value = programId + 1;
        }

    }
}
